﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.Lib;

namespace QuanLyBanHang.GUI
{
    public partial class frmTimKiem : Form
    {
        #region "Variables"
        DataTable dtbDanhMuc = new DataTable();
        DataTable dtb = new DataTable();
        string _strColumn;
        #endregion
        public frmTimKiem()
        {
            InitializeComponent();
            dtbDanhMuc = KetNoi.GetTable("select Table_name from INFORMATION_SCHEMA.TABLES");
        }

        private void frmTimKiem_Load(object sender, EventArgs e)
        {
            cmbDanhMuc.ValueMember = "Table_name";
            cmbDanhMuc.DisplayMember = "Table_name";
            cmbDanhMuc.DataSource = dtbDanhMuc;
        }

        private void cmbDanhMuc_SelectedValueChanged(object sender, EventArgs e)
        {
            
            if (cmbDanhMuc.SelectedValue != null)
            {
                dtb = KetNoi.GetTable("select * from " + cmbDanhMuc.SelectedValue.ToString() + "");
                dtgTimKiem.DataSource = dtb;
                dtgTimKiem.AllowUserToAddRows = false;
                _strColumn = dtb.Columns[1].ToString();
            }
            //DataRowView row = (DataRowView)cmbDanhMuc.SelectedItem;
            // _str = row["Table_name"].ToString().Trim();
            
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            
            if (txtTimKiem.Text != "")
            {
                dtb = KetNoi.GetTable("select * from " + cmbDanhMuc.SelectedValue.ToString() + " where "+_strColumn+" like '%"+txtTimKiem.Text+"%'");
                dtgTimKiem.DataSource = dtb;
                dtgTimKiem.AllowUserToAddRows = false;
            }
        }
    }
}
